/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package managedBeans.util;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author REX
 */
public class JsfUtil {
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "---");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }
    public static void addErrorMessage(String indexBundle) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        Locale locale;
        locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("/mensajes/Mensajes", locale );
        FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,rb.getString(indexBundle), "error");
        ctx.addMessage("sample:"+indexBundle, fm);
    }

    public static void addSuccessMessage(String indexBundle) {
       FacesContext ctx = FacesContext.getCurrentInstance();
       Locale locale;
       locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
       ResourceBundle rb = ResourceBundle.getBundle("/mensajes/Mensajes", locale );
       FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO,rb.getString(indexBundle), "exito");
       ctx.addMessage("sample:"+indexBundle, fm);
    }

}
